package com.hzcx.app.simplechat.ui.chat;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.chat.adapter.ChatHistoryAdapter;
import com.hzcx.app.simplechat.ui.chat.bean.ChatHistoryBean;
import com.hzcx.app.simplechat.ui.chat.contract.ChatHistoryContract;
import com.hzcx.app.simplechat.ui.chat.presenter.ChatHistoryPresenter;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.view.autogridview.PhotoLoader;
import com.hzcx.app.simplechat.view.autogridview.PhotoProgressUi;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import indi.liyi.viewer.ImageViewer;

/**
 * 历史聊天记录
 */
public class ChatHistoryActivity extends BaseActivity<ChatHistoryPresenter> implements ChatHistoryContract.View {

    public static final String INTENT_NICKNAME = "INTENT_NICKNAME";
    public static final String INTENT_USER_ID = "INTENT_USER_ID";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rv_history)
    RecyclerView rvHistory;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    @BindView(R.id.imageViewer)
    ImageViewer imageViewer;

    private int userId;
    private String msgId;
    private List<ChatHistoryBean> histiryData;
    private ChatHistoryAdapter historyAdapter;

    @Override
    public int getLayout() {
        return R.layout.activity_chat_history;
    }

    @Override
    public void initPresenter() {
        mPresenter = new ChatHistoryPresenter();
    }

    @Override
    public void initView() {
        imageViewer.imageLoader(new PhotoLoader());
        tvTitle.setText(getIntent().getStringExtra(INTENT_NICKNAME));
        userId = getIntent().getIntExtra(INTENT_USER_ID, 0);

        histiryData = new ArrayList<>();
        historyAdapter = new ChatHistoryAdapter(histiryData);
        rvHistory.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvHistory.setAdapter(historyAdapter);
    }

    @Override
    public void initData() {

        mPresenter.getChatHistory(this, userId, msgId, true);

        swipeRefresh.setOnRefreshListener(() -> {
            try {
                if (histiryData != null && histiryData.size() > 0) {
                    msgId = histiryData.get(0).getId() + "";
                }
                mPresenter.getChatHistory(ChatHistoryActivity.this, userId, msgId, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        historyAdapter.setOnImageOnClickListener((pos, url) -> showImage(pos, url));
    }

    /**
     * 展示图片
     *
     * @param pos
     * @param url
     */
    private void showImage(int pos, String url) {
        List<String> imgList = new ArrayList<>();
        for (ChatHistoryBean bean : histiryData) {
            if (EmptyUtils.isNotEmpty(bean.getUrlimage())) {
                imgList.add(bean.getUrlimage());
            }
        }
        int index = 0;
        for (int i = 0; i < imgList.size(); i++) {
            if (url.equals(imgList.get(i))) {
                index = i;
            }
        }
        imageViewer.overlayStatusBar(true)
                .imageData(imgList)
                .loadProgressUI(new PhotoProgressUi())
                .watch(index);
    }

    /**
     * 历史消息获取成功
     *
     * @param data
     */
    @Override
    public void historyResult(List<ChatHistoryBean> data) {
        try {
            swipeRefresh.setRefreshing(false);
            if (EmptyUtils.isEmpty(msgId)) {
                histiryData.addAll(data);
                historyAdapter.notifyDataSetChanged();
                rvHistory.scrollToPosition(histiryData.size() - 1);
            } else {
                historyAdapter.addData(0, data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFail() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
